﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraContractMangeDetail : System.Web.UI.Page
{
    string _strProjectIdDeltail;
    string _strGoodsid = "", _AreaId="";
    public string _AreaIdDeltail = "";
    string _strHopitalIdDeltail,_strTbridDeltail,_strPsDeltail,_strScDeltail,_strState;
    protected void Page_Load(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            _AreaIdDeltail = SysFun.ToTrim(_Account.AreaId);
            _strHopitalIdDeltail = SysFun.IsSQL(Request.QueryString["AccountId_YY"]);
            _strTbridDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_TB"]));
            _strPsDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_PS"]));
            _strScDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_SC"]));
            _strProjectIdDeltail =SysFun.IsSQL(Request.QueryString["ProjectId"]);
            _strState = SysFun.IsSQL(Request.QueryString["State"]);
            _strGoodsid = SysFun.IsSQL(Request.QueryString["Goodsid"]);
            _AreaId = SysFun.IsSQL(Request.QueryString["AreaId"]);
            switch (_strState)
            {
                case "0":

                    break;
                case "1":
                    txtCompanyName_TB.Enabled = false;
                    txtUserCode_TB.Enabled = false;
                    break;
                case "2":

                    break;
                case "3":
                    break;

            }
            showGridView();

        }

    }


    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "PurchaseTable";
        switch (_strState)
        { 
            case "0": //原中标
                dbPage.SelectFieldGroup = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,GoodsState";                                       //要查询的表明
               
                dbPage.SelectField = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,Sum(PurchasePrice*Isnull(PlanPurNum,0)) AS SumContractMoney,GoodsState";//要显示的字段
              
                dbPage.Key = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId";
                dbPage.OrderBy = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";//排序字段 
                break;
            case "1"://备案采购
                dbPage.SelectFieldGroup = "UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,GoodsState";                                       //要查询的表明
               
                dbPage.SelectField = "UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,Sum(PurchasePrice*Isnull(PlanPurNum,0)) AS SumContractMoney,GoodsState";//要显示的字段
              
                dbPage.Key = "CompanyName_PS,AccountId_PS,AccountId_YY,HospitalName,UserCode_YY,ProjectId";
                dbPage.OrderBy = "CompanyName_PS,AccountId_PS,AccountId_YY,ProjectId,print_state";//排序字段
                break;
               break;
           case "2"://不限不竞
               dbPage.SelectFieldGroup = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,GoodsState";                                       //要查询的表明
              
               dbPage.SelectField = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,Sum(PurchasePrice*Isnull(PlanPurNum,0)) AS SumContractMoney,GoodsState";//要显示的字段
              
               dbPage.Key = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId";
               dbPage.OrderBy = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";//排序字段
               break;
           case "3"://全部
               dbPage.SelectFieldGroup = "Goodsid,UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,GoodsState";                                       //要查询的表明

               dbPage.SelectField = "Goodsid,UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId,print_state,Sum(PurchasePrice*Isnull(PlanPurNum,0)) AS SumContractMoney";//要显示的字段

               dbPage.Key = "Goodsid,CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,HospitalName,UserCode_YY,ProjectId";
               dbPage.OrderBy = "Goodsid,CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";//排序字段
               break;

        }
                                                   //表的主键    
        //dbPage.Key = fctKey();
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);         //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        
        //dbPage.OrderBy = fctOrderBy();

        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='right' cellpadding='2' cellspacing='1'>");
        strHtml.Append("<tr>");
        strHtml.Append("<table width='98%' border='0' align='right' cellpadding='2' cellspacing='1'>");
        strHtml.Append("<tr>");
        if ((_strState == "3") && (_strGoodsid != ""))
        {
            strHtml.Append("<td width='8%'>流水号</td>");
        }
        strHtml.Append("<td width='8%'>医院注册号</td>");
        strHtml.Append("<td width='18%'>医院名称</td>");
        switch (_strState)
        {
            case "0":
                strHtml.Append("<td width='8%'>中标人注册号</td>");
                strHtml.Append("<td width='18%'>中标人名称</td>");
                break;
            case "1":
                break;
            case "2":
                strHtml.Append("<td width='8%'>中标人注册号</td>");
                strHtml.Append("<td width='18%'>中标人名称</td>");
                break;
            case "3":
                strHtml.Append("<td width='8%'>中标人注册号</td>");
                strHtml.Append("<td width='18%'>中标人名称</td>");
                break;

        }
       
        strHtml.Append("<td width='8%'>配送企业注册号</td>");
        strHtml.Append("<td width='18%'>配送企业名称</td>");
        strHtml.Append("<td width='10%'>查看合同</td>");
        strHtml.Append("<td width='10%'>查看合同附表</td>");
        strHtml.Append("<td width='6%'>合同金额</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);

        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
            if ((_strState == "3") && (_strGoodsid != ""))
            {
                strHtml.Append("<td> " + dr["Goodsid"].ToString() + " </td>");
            }
            strHtml.Append("<td> " + dr["UserCode_YY"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["HospitalName"].ToString() + " </td>");
            switch (_strState)
            {
                case "0":
                    strHtml.Append("<td> " + dr["UserCode_TB"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_TB"].ToString() + "' target='_blank'>" + dr["CompanyName_TB"].ToString() + "</a></td>");
                    strHtml.Append("<td>" + dr["UserCode_PS"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='blank' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a></td>");
                    strHtml.Append("<td><a href='../Hospital/Print.aspx?AccountId_YY=" + dr["AccountId_YY"].ToString() + "&CompanyName_TB=" + dr["CompanyName_TB"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&AccountId_PS=" + dr["AccountId_PS"].ToString() + "&CompanyName_PS=" + dr["CompanyName_PS"].ToString() + "&Projectid=" + _strProjectIdDeltail + "' target='_blank' >查看合同</a>");
                    break;
                case "1":
                    strHtml.Append("<td>" + dr["UserCode_PS"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='blank' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a></td>");
                    strHtml.Append("<td><a href='../Hospital/Print.aspx?AccountId_YY=" + dr["AccountId_YY"].ToString() + "&CompanyName_TB=&AccountId_TB=&AccountId_PS=" + dr["AccountId_PS"].ToString() + "&CompanyName_PS=" + dr["CompanyName_PS"].ToString() + "&Projectid=" + _strProjectIdDeltail + "' target='_blank' >查看合同</a>");
                    break;
                case "2":
                    strHtml.Append("<td> " + dr["UserCode_TB"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_TB"].ToString() + "' target='_blank'>" + dr["CompanyName_TB"].ToString() + "</a></td>");
                    strHtml.Append("<td>" + dr["UserCode_PS"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='blank' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a></td>");
                    strHtml.Append("<td><a href='../Hospital/Print.aspx?AccountId_YY=" + dr["AccountId_YY"].ToString() + "&CompanyName_TB=" + dr["CompanyName_TB"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&AccountId_PS=" + dr["AccountId_PS"].ToString() + "&CompanyName_PS=" + dr["CompanyName_PS"].ToString() + "&Projectid=" + _strProjectIdDeltail + "' target='_blank' >查看合同</a>");
                    break;
                case "3":
                    strHtml.Append("<td> " + dr["UserCode_TB"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_TB"].ToString() + "' target='_blank'>" + dr["CompanyName_TB"].ToString() + "</a></td>");
                    strHtml.Append("<td>" + dr["UserCode_PS"].ToString() + " </td>");
                    strHtml.Append("<td><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='blank' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a></td>");
                    strHtml.Append("<td><a href='../Hospital/Print.aspx?AccountId_YY=" + dr["AccountId_YY"].ToString() + "&CompanyName_TB=" + dr["CompanyName_TB"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&AccountId_PS=" + dr["AccountId_PS"].ToString() + "&CompanyName_PS=" + dr["CompanyName_PS"].ToString() + "&Projectid=" + _strProjectIdDeltail + "' target='_blank' >查看合同</a>");
                        break;

            }
            
           
            if ((dr["print_state"]).ToString() != "")
            {
                strHtml.Append("<td><a href='../Hospital/PrintPurList.aspx?HospitalName=" + dr["HospitalName"].ToString() + "&AccountId_YY=" + dr["AccountId_YY"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&Accountid_PS=" + dr["AccountId_PS"].ToString() + "&goodsid=" + _strGoodsid + "&ProjectId=" + _strProjectIdDeltail + "' target='_blank' title='已打印'><font color= red>查看合同附表</font></a>");
            }
            else
            {
                strHtml.Append("<td><a href='../Hospital/PrintPurList.aspx?HospitalName=" + dr["HospitalName"].ToString() + "&AccountId_YY=" + dr["AccountId_YY"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&Accountid_PS=" + dr["AccountId_PS"].ToString() + "&goodsid=" + _strGoodsid + "&ProjectId=" + _strProjectIdDeltail + "' target='_blank' title='未打印'>查看合同附表</a>");
            }
            strHtml.Append("<td> " + dr["SumContractMoney"].ToString() + " </td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }


    public string fctConditions()
    {
        string _strTemp = "";
        if (_strState == "3")
        {
            if ((_strGoodsid != "")&&(_strGoodsid !=null))
            {
                _strTemp = " ProjectId='" + _strProjectIdDeltail + "' And Goodsid=" + _strGoodsid +" and";
            }
            else if ((_AreaId !="")&&(_AreaId !=null))
            {
                int _intAreaid = SysFun.ToInt(_AreaId), _intAreaIdMax = 0;
               
                if (_intAreaid != 530000)
                {
                    if (_intAreaid % 100 > 0)
                    {
                        _strTemp += " ProjectId='" + _strProjectIdDeltail + "' and PurchaseTable.areaid=" + _intAreaid.ToString() + " and";
                    }
                    else
                    {
                        _intAreaIdMax = _intAreaid + 100;
                        _strTemp += " ProjectId='" + _strProjectIdDeltail + "' and PurchaseTable.areaid>=" + _intAreaid.ToString() + " And PurchaseTable.AreaId< " + _intAreaIdMax.ToString() + " and";
                    }
                }
            }
            else
            {
                _strTemp = " ProjectId='" + _strProjectIdDeltail + "' And";
            }
        }
        else
        {
             _strTemp = " ProjectId='" + _strProjectIdDeltail + "' and GoodsState=" + _strState + "  And";
        }
        //投标人名称
        string _strCompanyName_TB = this.txtCompanyName_TB.Text.ToString();
        //投标人ID
        if (_strTbridDeltail != "") 
        {
            _strTemp += " AccountId_TB=" + _strTbridDeltail + " And";
        }
        if (_strCompanyName_TB != "")
        {
            _strTemp += " CompanyName_TB like '%" + _strCompanyName_TB + "%' and";
        }
        //投标人编码
        string _strUserCode_TB = this.txtUserCode_TB.Text.ToString();
        if (_strUserCode_TB != "")
        {
            _strTemp += " UserCode_TB like '%" + _strUserCode_TB + "%' and";
        }
        //配送企业ID
        if (_strPsDeltail != "")
        {
            _strTemp += " AccountId_PS=" + _strPsDeltail + " And";
        }
        //配送企业名称
        string _strCompanyName_PS = this.txtCompanyName_PS.Text.ToString();
        if (_strCompanyName_PS != "")
        {
            _strTemp += " CompanyName_PS like '%" + _strCompanyName_PS + "%' and";
        }

        //配送企业编码
        string _strUserCode_PS = this.txtUserCode_PS.Text.ToString();
        if (_strUserCode_PS != "")
        {
            _strTemp += " UserCode_PS like '%" + _strUserCode_PS + "%' and";
        }
        //医院ID
        if (_strHopitalIdDeltail != "")
        {
            _strTemp += " AccountId_YY=" + _strHopitalIdDeltail + " And";
        }
        //医院名称
        string _strHospitalName = this.txtHospitalName.Text.ToString();
        if (_strHospitalName != "")
        {
            _strTemp += "  HospitalName like '%" + _strHospitalName + "%' and";
        }

        //医院编码
        string _strUserCode_YY = this.txtUserCode_YY.Text.ToString();
        if (_strUserCode_YY != "")
        {
            _strTemp += " UserCode_YY like '%" + _strUserCode_YY + "%' and";
        }
        //生产企业ID
        if (_strScDeltail != "")
        {
            _strTemp += " AccountId_SC=" + _strScDeltail + " And";
        }
        //生产企业名称
        string _strCompanyName_sc = this.txtCompanyName_sc.Text.ToString();
        if (_strCompanyName_sc != "")
        {
            _strTemp += " CompanyName_sc  like '%" + _strCompanyName_sc + "%' and";
        }
        //生产企业编码
        string _strUserCode_SC = this.txtUserCode_SC.Text.ToString();
        if (_strUserCode_SC != "")
        {
            _strTemp += " UserCode_SC like '%" + _strUserCode_SC + "%' and";
        }
        //地区
        _strTemp += SysFun.AreaIdWhere(_AreaIdDeltail);

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }

        return _strTemp;
    }

}
